package com.loongtech.bi.action.monitor;

import com.loongtech.bi.action.authManagement.ResultData;
import com.loongtech.bi.entity.POJO.Page;
import com.loongtech.bi.entity.system.EntityDaliyReport;
import com.loongtech.bi.manager.system.DailyReportManager;
import com.loongtech.bi.manager.system.SyncInfoLogManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.synch.MonitorMailManager;
import com.loongtech.core.util.RetCode;
import com.loongtech.core.util.Time;
import com.loongtech.core.util.Utils;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Author: yuri
 * Description: 日报配置
 * Date: 16:43 2017/5/17
 */
@RestController
@Scope("prototype")
@RequestMapping("/dailyreport")
public class DailyReportAction {
    @Resource
    private DailyReportManager dailyReportManager;
    @Resource
    private MonitorMailManager monitorMailManager;
    @Resource
    private Session session;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private SyncInfoLogManager syncInfoLogManager;

    /**
     * 查询
     */
    @RequestMapping(value = "list.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> searchByCriteria(@RequestParam Integer curPage, @RequestParam Integer pageSize, EntityDaliyReport entity) {
        Page<List<EntityDaliyReport>> list;
        try {
            list = dailyReportManager.queryByCriteria(curPage, pageSize, entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), list);
    }

    /**
     * 增加
     */
    @RequestMapping(value = "add.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> addEntityDaliyReport(EntityDaliyReport entity) {
        if (entity.getGameId() == null) {
            return new ResultData<>(RetCode.eEmptyId.getErrorcode(), RetCode.eEmptyId.getErrorMsg());
        }
        if (Utils.isEmptyStr(entity.getInquerySQLOne()) || Utils.isEmptyStr(entity.getInquerySQLTwo())) {
            return new ResultData<>(RetCode.eEmptyQuery.getErrorcode(), RetCode.eEmptyQuery.getErrorMsg());
        }
        if (entity.getThreshHold() == null) {
            return new ResultData<>(RetCode.eEmptyThresh.getErrorcode(), RetCode.eEmptyThresh.getErrorMsg());
        }
        try {
            dailyReportManager.addEntityDaliyReport(entity);
            sysLogManager.addLog(session.getUserName(), "增加日报项成功:" + entity.getGameName());
//            syncInfoLogManager.add(entity.getGameId(), EnumTableName.dailyReport.getTableName());
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    /**
     * 删除
     */
    @RequestMapping(value = "delete.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> delEntityDaliyReport(@RequestParam(value = "id") Integer id) {
        //数据库无此id
        try {
            EntityDaliyReport cur = dailyReportManager.get(id);
            if (cur == null) {
                return new ResultData<>(RetCode.eNotExistGourp.getErrorcode(), RetCode.eNotExistGourp.getErrorMsg());
            }
            dailyReportManager.delEntityDaliyReport(id);
            sysLogManager.addLog(session.getUserName(), "日报项删除成功");
//            syncInfoLogManager.add(cur.getGameId(), EnumTableName.dailyReport.getTableName());
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    /**
     * 修改
     */
    @RequestMapping(value = "modify.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> modifyEntityDaliyReport(EntityDaliyReport entity) {
        if (entity.getGameId() == null) {
            return new ResultData<>(RetCode.eEmptyId.getErrorcode(), RetCode.eEmptyId.getErrorMsg());
        }
        if (Utils.isEmptyStr(entity.getInquerySQLOne()) || Utils.isEmptyStr(entity.getInquerySQLTwo())) {
            return new ResultData<>(RetCode.eEmptyQuery.getErrorcode(), RetCode.eEmptyQuery.getErrorMsg());
        }
        if (entity.getThreshHold() == null) {
            return new ResultData<>(RetCode.eEmptyThresh.getErrorcode(), RetCode.eEmptyThresh.getErrorMsg());
        }
        try {
            EntityDaliyReport cur = dailyReportManager.get(entity.getId());
            if (cur != null) {
                entity.setCreatTime(cur.getCreatTime());
                dailyReportManager.modifyEntityDaliyReport(entity);
                sysLogManager.addLog(session.getUserName(), "日报项修改成功");
//                syncInfoLogManager.add(entity.getGameId(), EnumTableName.dailyReport.getTableName());
                return ResultData.success();
            } else {
                return new ResultData<>(RetCode.eNotExistGourp.getErrorcode(), RetCode.eNotExistGourp.getErrorMsg());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }


    //发送一次昨日报表
    @RequestMapping(value = "test.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void test() {
        Date testDate = new Date(System.currentTimeMillis() - Time.DAY);
        System.out.println("test mail");
        System.out.println(Time.getDateStr(testDate));
        monitorMailManager.mail("zhenghaiyuan@zulong.com", "linkmeliu@163.com", Time.getDateStr(testDate));
    }
}
